package Dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Dao.EmployeeDao;
import Domain.Employee;
import util.JDBC;

public class EmployeeDaoImpl implements EmployeeDao{

	@Override
	public void AddEmployee(Employee emp) {
		String sql = "insert into employee values(?,?,?,?,?,?,?)";
		Connection conn= util.JDBC.getConnection();
		PreparedStatement ps=null;
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(0, emp.getId());
			ps.setString(1, emp.getName());
			ps.setString(2, emp.getSex());
			ps.setInt(3, emp.getAge());
			ps.setString(4, emp.getDapartment());
			ps.setString(5, emp.getType());
			ps.setInt(6, emp.getYear());
			int result=ps.executeUpdate();
			System.out.println(result);
			util.JDBC.close(ps, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		JDBC.close(ps, conn);
	}

	@Override
	public void DeleteEmployee(Employee emp) {
		String sql = "delete employee where id=?";
		Connection conn= util.JDBC.getConnection();
		try {
			PreparedStatement ps=conn.prepareStatement(sql);
			ps.setInt(0, emp.getId());
			int result=ps.executeUpdate();
			System.out.println(result);
			util.JDBC.close(ps, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public List<Employee> getEmployee() {
		List<Employee> emps=new ArrayList<>();
		String sql="select * from employee";
		Connection conn= util.JDBC.getConnection();
		PreparedStatement ps=null;
		ResultSet res=null;
		try {
			ps=conn.prepareStatement(sql);
			res=ps.executeQuery();
			while(res.next()) {
				Employee emp=new Employee();
				emp.setAge(res.getInt("id"));
				emp.setName(res.getString("name"));
				emp.setSex(res.getString("sex"));
				emp.setDapartment(res.getString("dapartment"));
				emp.setType(res.getString("type"));
				emp.setYear(res.getInt("year"));
				emp.setId(res.getInt("id"));
				emps.add(emp);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		JDBC.close(res, ps, conn);
		return emps;
	}

	@Override
	public Employee findEmployee(int id) {
		Employee emp=new Employee();
		String sql="select * from employee where id = ? ";
		Connection conn= util.JDBC.getConnection();
		ResultSet res=null;
		PreparedStatement ps = null;
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(0, id);
			res=ps.executeQuery();
			while(res.next()) {
				emp.setAge(res.getInt("id"));
				emp.setName(res.getString("name"));
				emp.setSex(res.getString("sex"));
				emp.setDapartment(res.getString("dapartment"));
				emp.setType(res.getString("type"));
				emp.setYear(res.getInt("year"));
				emp.setId(res.getInt("id"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBC.close(res, ps, conn);
		return emp;
	}

	@Override
	public void UpdateEmployee(Employee emp) {
		String sql = "update employee set id=?,name=?,sex=?,age=?,adpartment=?,type=?,year=?where id=?";
		Connection conn= util.JDBC.getConnection();
		PreparedStatement ps=null;
		try {
			ps=conn.prepareStatement(sql);
			ps.setInt(0, emp.getId());
			ps.setString(1, emp.getName());
			ps.setString(2, emp.getSex());
			ps.setInt(3, emp.getAge());
			ps.setString(4, emp.getDapartment());
			ps.setString(5, emp.getType());
			ps.setInt(6, emp.getYear());
			ps.setInt(7, emp.getId());
			int result=ps.executeUpdate();
			System.out.println(result);
			util.JDBC.close(ps, conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}	
		JDBC.close(ps, conn);
	}
}
